import allure
from selenium.webdriver.chrome.service import Service
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from common.sql import MysqlAuto
from common.logger import log
from settings import ENV,DBsql


# @pytest.fixture(scope = "class")
# def login():
#     #初始化浏览器
#
#     #打开浏览器，设置隐式等待时间5s，最大化窗口
#     service =   Service("../driver/chromedriver.exe")
#     driver = webdriver.Chrome(service = service)
#     log.debug("打开浏览器")
#     log.debug("最大化窗口")
#     driver.implicitly_wait(5)
#     driver.maximize_window()
#
#     #初始查询数据库
#     try:
#         mysql_instance = MysqlAuto()
#         mysql_instance.execute(DBsql.sql_list)
#         log.info("初始化数据库成功")
#     except Exception as init_error:
#         log.error(f"数据库初始化失败{init_error}")
#
#
#
#     #打开优香农页面
#     driver.find_element(By.XPATH,"//span[contains(text(),'立即体验')]").click()
#     handles = driver.window_handles
#     driver.switch_to.window(handles[-1])
#
#     #返回驱动对象给用例
#     yield driver
#
#
#     driver.quit()
#     log.debug("关闭浏览器")


@pytest.fixture(scope = "class")
def driver():
    #初始化Chrom浏览器
    log.info("浏览器驱动启动成功")
    service = Service("driver/chromedriver.exe")#获取浏览器驱动文件
    driver = webdriver.Chrome(service = service)#初始化驱动
    driver.maximize_window()#窗口最大化

    #返回浏览器驱动实例
    yield driver

    #关闭浏览器驱动
    driver.quit()
    log.info("浏览器驱动关闭成功")
